#! /usr/bin/env python
# -*- coding: utf-8

##	  Copyright 2012, Geoffrey GROFF and Dimitri SEGARD
##	  This file is part of otaku-loader.
##
##    otaku-loader is free software: you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation, either version 3 of the License, or
##    (at your option) any later version.
##
##    otaku-loader is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with otaku-loader.  If not, see <http://www.gnu.org/licenses/>.

import xml.etree.ElementTree as etree
import globales
import pprint


def getAnimes():
	data = []
	tree = etree.parse('animes.xml')
	root = tree.getroot()
	for child in root:
		data.append({'id':child.find('id').text,
			     'nom':child.find('nom').text,
			     'image':child.find('image').text,
			     'fiche':child.find('fiche').text})

	return data
	
def getFiches():
	data = []
	tree = etree.parse('animes.xml')
	root = tree.getroot()
	for child in root:
		data.append(child.find('fiche').text)

	return data

def getFicheInfos(fiche):
##	print('[*] Get info : '+fiche)
	globales.dl.getFicheInfos(fiche)
	
	#simpleTags = ['titre', 'total', 'enligne', 'licencie', 'emplacement', 'description']
	simpleTags = ['id', 'titre', 'description', 'image']

	filename = 'fiches/' + fiche[0:fiche.rfind('.')]+'.xml'

	try:
		tree = etree.parse(filename)
	except :
		return []
		
	root = tree.getroot()

	data = { 'id':'',
			 'titre':'',
			 'description':'',
			 'image':'',
			 'episodes':[],
			 'bonus':[],
			 'genres':[]}

	for child in root:
		if child.tag == 'genres':
			genres = []			
			for genre in child.findall('genre'):
					genres.append(genre.text)					
			data['genres'] = genres
		#elif child.tag in ['team', 'image']:
		#	data.append([child.find('nom').text, child.find('url').text])
		elif child.tag == 'episodes':
			episodes = child.findall('episode')
			listeEpisodes = []			

			for episode in episodes:
				listeEpisodes.append({'id':episode.find('id').text,
									  'numero':episode.find('numero').text, 
									  'encodage':episode.find('encodage').text,
									  'resolution':episode.find('resolution').text,
									  'duree':episode.find('duree').text,
									  'taille':episode.find('taille').text,
									  'upload':episode.find('upload').text,
									  'nombre_de_dl':episode.find('nombre_de_dl').text})			
			data['episodes'] = listeEpisodes
		elif child.tag == 'bonus':
			oavs = child.findall('oav')
			listeBonus = []			

			for oav in oavs:
				listeBonus.append({'id':oav.find('id').text,
									  'nom':oav.find('nom').text, 
									  'taille':oav.find('taille').text,
									  'upload':oav.find('upload').text,
									  'nombre_de_dl':oav.find('nombre_de_dl').text})			
			data['bonus'] = listeBonus
		else:
			if child.tag in simpleTags:
				for elem in simpleTags:
					if child.tag == elem:
						data[elem] = child.text
	
	return data
	
#Exemple	
##pprint.pprint(getFicheInfos('fiche_serie-108.html'))
